<?php

namespace falcon5\inc\Helpers;

use falcon5\inc\Base\Common;
use falcon5\inc\Base\WPOption;
use falcon5\inc\Base\WPUrlMode;
use lucatume\DI52\ContainerException;

class HttpResponseHelper
{
    /**
     * 输出响应头
     * @param array $responseHeaders
     * @param string $host
     * @param string $lang
     */
    public static function echoHeaders(array $responseHeaders, string $host, string $lang)
    {
        foreach($responseHeaders as $key => $value) {
            $header = sprintf("%s: %s", $key, implode(',', $value));
            $response_headers = [];

            if(!empty(trim($header)) && !preg_match('/Content\-Length:|Transfer\-Encoding:|Content\-Encoding:|Link:/i', $header)) {
                // 不再处理 Location 和 Refresh 头，让 Proxy 类处理重定向
                if(!preg_match('/^(Location|Refresh):/i', $header)) {
                    // woocommerce cookie path fix
                    if(preg_match('/^Set-Cookie:/i', $header) && strpos($header, 'woocommerce') !== false) {
                        $header = preg_replace('/path=\/.*\/;/', 'path=/;', $header);
                    }

                    $response_headers[$key] = $header;
                }
            }

            if ($response_headers) {
                foreach($response_headers as $header) {
                    header($header, false);
                }
            }
        }
    }

    /**
     * 处理并输出HTML内容
     * @param string $html
     * @param string $fc5lang
     * @return string
     * @throws ContainerException
     */
    public static function echoHtml(string $html, string $fc5lang): string
    {
        /** @var WPOption $falcon5Option */
        $falcon5Option = falcon5_app(WPOption::class);
        $urlMode = $falcon5Option->getUrlMode();

        // 只在子目录模式下进行URL替换
        if ($urlMode === WPUrlMode::URL_MODE_SUBFOLDER) {
            // 替换 href 属性
            $html = str_ireplace('href="/', 'href="/' . $fc5lang . '/', $html);
            $html = preg_replace('/href=\"\/' . $fc5lang . '\/(' . Common::LANG_PATTERN . ')\//i', 'href="/$1/', $html);
            $html = str_ireplace('href="/' . $fc5lang . '//', 'href="//', $html);
            
            // 替换 action 属性
            $html = str_ireplace('action="/', 'action="/' . $fc5lang . '/', $html);
            $html = str_ireplace('action=\'/', 'action=\'/' . $fc5lang . '/', $html);
            $html = str_ireplace('action="/' . $fc5lang . '//', 'action="//', $html);
            $html = str_ireplace('action=\'/' . $fc5lang . '//', 'action=\'//', $html);
            $html = str_ireplace('action="//' . $_SERVER['HTTP_HOST'], 'action="//' . $_SERVER['HTTP_HOST'] . '/' . $fc5lang, $html);
            $html = str_ireplace('action=\'//' . $_SERVER['HTTP_HOST'], 'action=\'//' . $_SERVER['HTTP_HOST'] . '/' . $fc5lang, $html);

            // woocommerce specific changes
            $html = str_ireplace(
                ['ajax_url":"\\/', '"checkout_url":"\\/'],
                ['ajax_url":"\\/'.$fc5lang.'\\/', '"checkout_url":"\\/'.$fc5lang.'\\/'],
                $html
            );
        }

        if(function_exists('gzencode') && isset($return_gz) && $return_gz && !zlib_get_coding_type()) {
            return gzencode($html);
        }

        return $html;
    }
}